package com.ruoyi.push;

import com.ruoyi.push.android.*;
import com.ruoyi.push.ios.*;
import jakarta.annotation.Resource;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
public class PushService {
    @Resource
    private UmPushConfig umPushConfig;

    private final PushClient client = new PushClient();


    /**
     * 发送广播
     * @param ticker
     * @param title
     * @param text
     * @param type
     * @throws Exception
     */
    public void sendAndroidBroadcast(String ticker, String title, String text, int type) throws Exception {
        String appkeyT = umPushConfig.getAppkey();
        String appMasterSecretT =  umPushConfig.getAppMasterSecret();
        AndroidBroadcast broadcast = new AndroidBroadcast(appkeyT, appMasterSecretT);
//        broadcast.setTicker( "Android broadcast ticker");
        broadcast.setTicker(ticker);
//        broadcast.setTitle(  "中文的title");
        broadcast.setTitle(title);
//        broadcast.setText(   "Android broadcast text");
        broadcast.setText(text);
        broadcast.goAppAfterOpen();
        broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        broadcast.setProductionMode();
        // Set customized fields
//        broadcast.setExtraField("test", "helloworld");
//        //厂商通道相关参数
//        broadcast.setChannelActivity("your channel activity");
//        broadcast.setChannelProperties("abc");
        client.send(broadcast);
    }

    /**
     * 发送单播
     * @param device_token 设备token
     * @param ticker       提示文字
     * @param title        标题
     * @param text         内容
     * @param type         0教师端，1家长端
     * @throws Exception
     */
    public void sendAndroidUnicast(String device_token, String ticker, String title, String text, int type) throws Exception {
        //当前登录账号--》发送的应用（账号）类型
        String appkeyT = umPushConfig.getAppkey();
        String appMasterSecretT = umPushConfig.getAppMasterSecret();
        AndroidUnicast unicast = new AndroidUnicast(appkeyT, appMasterSecretT);
        // TODO Set your device token
        unicast.setDeviceToken(device_token);
        unicast.setTicker(ticker);
        unicast.setTitle(title);
        unicast.setText(text);
        unicast.goAppAfterOpen();
        unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        unicast.setProductionMode();
//        unicast.setTestMode();
        // Set customized fields
//        unicast.setExtraField("test", "helloworld");
//        unicast.setChannelActivity("your channel activity");
//        unicast.setChannelProperties("abc");
        client.send(unicast);
    }

    public void sendAndroidGroupcast() throws Exception {
        AndroidGroupcast groupcast = new AndroidGroupcast(umPushConfig.getAppkey(), umPushConfig.getAppMasterSecret());
        /*  TODO
         *  Construct the filter condition:
         *  "where":
         *	{
         *		"and":
         *		[
         *			{"tag":"test"},
         *			{"tag":"Test"}
         *		]
         *	}
         */
        JSONObject filterJson = new JSONObject();
        JSONObject whereJson = new JSONObject();
        JSONArray tagArray = new JSONArray();
        JSONObject testTag = new JSONObject();
        JSONObject TestTag = new JSONObject();
        testTag.put("tag", "test");
        TestTag.put("tag", "Test");
        tagArray.put(testTag);
        tagArray.put(TestTag);
        whereJson.put("and", tagArray);
        filterJson.put("where", whereJson);

        groupcast.setFilter(filterJson);
        groupcast.setTicker("Android groupcast ticker");
        groupcast.setTitle("中文的title");
        groupcast.setText("Android groupcast text");
        groupcast.goAppAfterOpen();
        groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        groupcast.setChannelActivity("your channel activity");
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        groupcast.setProductionMode();
        //厂商通道相关参数
        groupcast.setChannelActivity("your channel activity");
        groupcast.setChannelProperties("abc");
        client.send(groupcast);
    }

    /**
     *
     * 自定义别名推送
     * @param alias 规则 chongta_1;PushConstant.alias+id
     * @param aliasType chongtaType;PushConstant.aliasType
     * @throws Exception
     */
    @Async
    public void sendAndroidCustomizedcast(String alias,String aliasType,String ticker,String title,String text) throws Exception {
        AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(umPushConfig.getAppkey(), umPushConfig.getAppMasterSecret());
        customizedcast.setAlias(alias, aliasType);
        //Android customizedcast ticker
        customizedcast.setTicker(ticker);
        //中文的title
        customizedcast.setTitle(title);
        customizedcast.setText(text);
        customizedcast.goAppAfterOpen();
        customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        customizedcast.setProductionMode();
        //厂商通道相关参数
//        customizedcast.setChannelActivity("your channel activity");
//        customizedcast.setChannelProperties("abc");
        client.send(customizedcast);
    }

    public void sendAndroidCustomizedcastFile() throws Exception {
        String appkey = umPushConfig.getAppkey();
        String appMasterSecret = umPushConfig.getAppMasterSecret();
        AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey, appMasterSecret);
        // TODO Set your alias here, and use comma to split them if there are multiple alias.
        // And if you have many alias, you can also upload a file containing these alias, then
        // use file_id to send customized notification.
        String fileId = client.uploadContents(appkey, appMasterSecret, "aa" + "\n" + "bb" + "\n" + "alias");
        customizedcast.setFileId(fileId, "alias_type");
        customizedcast.setTicker("Android customizedcast ticker");
        customizedcast.setTitle("中文的title");
        customizedcast.setText("Android customizedcast text");
        customizedcast.goAppAfterOpen();
        customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        customizedcast.setProductionMode();
        //厂商通道相关参数
        customizedcast.setChannelActivity("your channel activity");
        customizedcast.setChannelProperties("abc");
        client.send(customizedcast);
    }

    public void sendAndroidFilecast() throws Exception {
        String appkey = umPushConfig.getAppkey();
        String appMasterSecret = umPushConfig.getAppMasterSecret();
        AndroidFilecast filecast = new AndroidFilecast(appkey, appMasterSecret);
        // TODO upload your device tokens, and use '\n' to split them if there are multiple tokens
        String fileId = client.uploadContents(appkey, appMasterSecret, "aa" + "\n" + "bb");
        filecast.setFileId(fileId);
        filecast.setTicker("Android filecast ticker");
        filecast.setTitle("中文的title");
        filecast.setText("Android filecast text");
        filecast.goAppAfterOpen();
        filecast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        //厂商通道相关参数
        filecast.setChannelActivity("your channel activity");
        filecast.setChannelProperties("abc");
        client.send(filecast);
    }

    /**
     * 发送广播
     * @param ticker
     * @param title
     * @param text
     * @param type
     * @throws Exception
     */
    public void sendIOSBroadcast(String ticker, String title, String text, int type) throws Exception {
        String appkeyT = umPushConfig.getAppkeyIos();
        String appMasterSecretT = umPushConfig.getAppMasterSecretIos();
        IOSBroadcast broadcast = new IOSBroadcast(appkeyT, appMasterSecretT);
        //alert值设置为字符串
        //broadcast.setAlert("IOS 广播测试");
        //alert的值设置为字典
        broadcast.setAlert(title, ticker, text);
        broadcast.setBadge(0);
        broadcast.setSound("default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
        broadcast.setProductionMode();
        // Set customized fields
//        broadcast.setCustomizedField("test", "helloworld");
        client.send(broadcast);
    }

    /**
     * 发送单播
     * @param device_token 设备token
     * @param ticker       提示文字
     * @param title        标题
     * @param text         内容
     * @param type         0教师端，1家长端
     * @throws Exception
     */
    public void sendIOSUnicast(String device_token, String ticker, String title, String text, int type) throws Exception {
        String appkeyT = umPushConfig.getAppkeyIos();
        String appMasterSecretT = umPushConfig.getAppMasterSecretIos();
        IOSUnicast unicast = new IOSUnicast(appkeyT, appMasterSecretT);
        // TODO Set your device token
        unicast.setDeviceToken(device_token);
        //alert值设置为字符串
        //unicast.setAlert("IOS 单播测试");
        //alert的值设置为字典
        unicast.setAlert(title, ticker, text);
        unicast.setBadge(0);
        unicast.setSound("default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
//        unicast.setTestMode();
        unicast.setProductionMode();
        // Set customized fields
//        unicast.setCustomizedField("test", "helloworld");
        client.send(unicast);
    }


    public void sendIOSGroupcast() throws Exception {
        IOSGroupcast groupcast = new IOSGroupcast(umPushConfig.getAppkeyIos(), umPushConfig.getAppMasterSecretIos());
        /*  TODO
         *  Construct the filter condition:
         *  "where":
         *	{
         *		"and":
         *		[
         *			{"tag":"iostest"}
         *		]
         *	}
         */
        JSONObject filterJson = new JSONObject();
        JSONObject whereJson = new JSONObject();
        JSONArray tagArray = new JSONArray();
        JSONObject testTag = new JSONObject();
        testTag.put("tag", "iostest");
        tagArray.put(testTag);
        whereJson.put("and", tagArray);
        filterJson.put("where", whereJson);
        System.out.println(filterJson.toString());

        // Set filter condition into rootJson
        groupcast.setFilter(filterJson);
        //groupcast.setAlert("IOS 组播测试");
        //alert的值设置为字典
        groupcast.setAlert("今日天气", "subtitle", "今日可能下雨🌂");
        groupcast.setBadge(0);
        groupcast.setSound("default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
        groupcast.setTestMode();
        client.send(groupcast);
    }

    /**
     * 自定义别名推送
     * @param alias
     * @param aliasType
     * @throws Exception
     */
    @Async
    public void sendIOSCustomizedcast(String alias,String aliasType
    ,String ticker,String title,String text) throws Exception {
        IOSCustomizedcast customizedcast = new IOSCustomizedcast(umPushConfig.getAppkeyIos(), umPushConfig.getAppMasterSecretIos());
        // TODO Set your alias and alias_type here, and use comma to split them if there are multiple alias.
        // And if you have many alias, you can also upload a file containing these alias, then
        // use file_id to send customized notification.
        customizedcast.setAlias(alias, aliasType);
        //customizedcast.setAlert("IOS 个性化测试");
        //alert的值设置为字典
        //"今日可能下雨🌂"
        customizedcast.setAlert(title, ticker, text);
        customizedcast.setBadge(0);
        customizedcast.setSound("default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
        customizedcast.setProductionMode();
        client.send(customizedcast);
    }

    public void sendIOSFilecast() throws Exception {
        String appkey = umPushConfig.getAppkeyIos();
        String appMasterSecret = umPushConfig.getAppMasterSecretIos();
        IOSFilecast filecast = new IOSFilecast(appkey, appMasterSecret);
        // TODO upload your device tokens, and use '\n' to split them if there are multiple tokens
        String fileId = client.uploadContents(appkey, appMasterSecret, "aa" + "\n" + "bb");
        filecast.setFileId(fileId);
        //filecast.setAlert("IOS 文件播测试");
        //alert的值设置为字典
        filecast.setAlert("今日天气", "", "今日可能下雨🌂");
        filecast.setBadge(0);
        filecast.setSound("default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
        filecast.setTestMode();
        client.send(filecast);
    }
}
